﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.IO;
using Wyse.Server.Common;

namespace Wyse.Server.Base
{
    /// <summary>
    /// Specifies the action which should be
    /// taken afetr receiving data in a ClientContext
    /// </summary>
    public enum DataReceiveResult
    {
        /// <summary>
        /// We shoukd continue waiting for data from the client
        /// </summary>
        Continue,
        /// <summary>
        /// No more data will be read - the connection should be closed
        /// </summary>
        CloseConnection
    }

    /// <summary>
    /// Client context - stores information about a 
    /// single client connection
    /// </summary>
    public abstract class ClientContext
    {
        /// <summary>
        /// Creates a new client context
        /// </summary>
        public ClientContext(TcpClient client)
        {
            Client = client;
        }

        /// <summary>
        /// Event fired when the context is closed
        /// </summary>
        public event EventHandler Closed;

        /// <summary>
        /// Gets the TCP client
        /// </summary>
        protected TcpClient Client
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the socket
        /// </summary>
        protected Socket Socket
        {
            get { return Client.Client; }
        }

        /// <summary>
        /// Starts handling the client connection
        /// </summary>
        public abstract void Start();

        /// <summary>
        /// Closes the client connection
        /// </summary>
        public void Close()
        {
            Client.Close();

            if (Closed != null)
                Closed(this, EventArgs.Empty);
        }
    }
}
